package com.dbManage.page.note;

import com.dbManage.common.service.TagService;
import com.dbManage.dao.NoteDao;
import com.dbManage.entity.Message;
import com.dbManage.entity.Note;
import com.dbManage.exception.CommonException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class NoteController {
    @Resource
    NoteService noteService;
    @Resource
    TagService tagService;
    @Resource
    NoteDao noteDao;

    /**
     * 传送门列表
     * @param model
     * @return
     */
    @RequestMapping("/noteList")
    public String noteList(Model model) {
        model.addAttribute("tagMap", tagService.getTagMap());
        model.addAttribute("list", noteService.queryNote(null));
        return "noteList";
    }

    /**
     * 传送门筛选
     * @param note
     * @return
     */
    @RequestMapping("/queryNote")
    @ResponseBody
    public Message queryNote(Note note) {
        List<Note> list = noteService.queryNote(note);
        return new Message(Message.STATUS_SUCCESS, list, "success");
    }

    /**
     * 传送门添加页面
     * @param model
     * @return
     */
    @RequestMapping("/addNoteView")
    public String addNoteView(Model model) {
        model.addAttribute("tagMap", tagService.getTagMap());
        return "noteAdd";
    }

    /**
     * 添加传送门
     * @param note
     * @return
     */
    @RequestMapping("/addNote")
    @ResponseBody
    public Message addNote(Note note) {
        try {
            noteService.addNote(note);
        } catch (CommonException e) {
            return new Message(Message.STATUS_FAILED, e.getMessage());
        }
        return new Message(Message.STATUS_SUCCESS, "添加成功");
    }

    /**
     * 传送门修改页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/updateNoteView")
    public String updateNoteView(Model model, int id) {
        model.addAttribute("note", noteDao.findNoteById(id));
        model.addAttribute("tagMap", tagService.getTagMap());
        return "noteEdit";
    }

    /**
     * 修改传送门
     * @param note
     * @return
     */
    @RequestMapping("/updateNote")
    @ResponseBody
    public Message updateNote(Note note) {
        noteService.updateNote(note);
        return new Message(Message.STATUS_SUCCESS, "修改成功");
    }

    /**
     * 删除传送门
     * @param id
     * @return
     */
    @RequestMapping("/deleteNote")
    @ResponseBody
    public Message deleteNote(int id) {
        noteDao.deleteNoteById(id);
        return new Message(Message.STATUS_SUCCESS, "删除成功");
    }

    /**
     * 传送门审核列表
     * @param model
     * @return
     */
    @RequestMapping("/authList")
    public String authList(Model model) {
        Note note = new Note();
        note.setStatus(0);
        model.addAttribute("list",noteDao.queryNote(note));
        model.addAttribute("tagMap", tagService.getTagMap());
        return "authList";
    }

    /**
     * 传送门审核列表筛选
     * @param note
     * @return
     */
    @RequestMapping("/queryAuthNote")
    @ResponseBody
    public Message queryAuthNote(Note note) {
        note.setStatus(0);
        List<Note> list = noteService.queryNote(note);
        return new Message(Message.STATUS_SUCCESS, list, "success");
    }

    /**
     * 审核页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/authView")
    public String authView(Model model,int id) {
        model.addAttribute("note", noteDao.findNoteById(id));
        return "auth";
    }

    /**
     * 修改审核状态
     * @param note
     * @return
     */
    @RequestMapping("/auth")
    @ResponseBody
    public Message auth(Note note) {
        noteDao.updateNoteAuth(note);
        noteService.insertAuth(note);
        return new Message(Message.STATUS_SUCCESS,"修改成功");
    }


}
